/**
 * Created by Administrator on 2019/2/24.
 */
var noticeNo, noticeTitle, remark, startTime, endTime;
var total;
$(function () {
    initComponent();
    queryNoticeFunction();
});

function initComponent() {
    var curr_time = new Date();
    var strDate = curr_time.getFullYear() + "-";
    strDate += curr_time.getMonth() + 1 + "-";
    strDate += curr_time.getDate() + " 00:00:00";
    $("#startTime").datetimebox("setValue", strDate);
    $("#endTime").datetimebox("setValue", addDate(curr_time, 0) + " 23:59:59");
}

// 查询和重置
var queryNoticeFunction = function () {
    $("#queryNoticeBtn").click(function () {
        noticeNo = $("#noticeNo").val();
        noticeTitle = $("#noticeTitle").val();
        remark = $("#remark").val();
        startTime = $("#startTime").val();
        endTime = $("#endTime").val();
        reload();
        var links = $(".pagination-links");
        //console.log($("#pagination>table>tbody>tr>td:nth-of-type(8)").html());
    });
}

var clickCB = function (t) {
    //console.log(no);
    if($(t).prop("checked")==false) {// 这是点击过后的状态
        $.messager.confirm({title:"提示",msg:"是否取消显示？",
        left:"30px", fn:function (r) {
                if(r) {
                    var parameter = new Object();
                    parameter.noticeNo = "";
                    var jsonstr = postSyncAjax("/notice/changeDisplay", parameter);
                    //console.log(jsonstr);
                    if(jsonstr.success) {
                        $.messager.alert("提示","该通知已经隐藏！","info");
                        $(t).prop("checked",false);
                    } else {
                        $.messager.alert("提示","该通知隐藏失败："+jsonstr.msg,"warning");
                        $(t).prop("checked",true);
                    }
                } else {
                    $(t).prop("checked",true);
                }
            }});
    } else {
        $.messager.confirm({title:"提示",msg:"是否确定显示？",
            left:"30px", fn:function (r) {
            if(r) {
                var parameter = new Object();
                parameter.noticeNo = "";
                var jsonstr = postSyncAjax("/notice/changeDisplay", parameter);
                //console.log(jsonstr);
                if(jsonstr.success) {
                    $.messager.alert("提示","该通知已经显示！","info");
                    $(t).prop("checked",true);
                } else {
                    $.messager.alert("提示","该通知显示失败："+jsonstr.msg,"warning");
                    $(t).prop("checked",false);
                }
            } else {
                $(t).prop("checked",false);
            }
        }});
    }

}

// 删除通知
var delNT = function (id) {
    $.messager.confirm({title:"提示",msg:"是否删除该通知？",
        left:"30px", fn:function (r) {
            if(r) {
                var parameter = new Object();
                parameter.id = id;
                var jsonstr = postSyncAjax("/notice/delete", parameter);
                //console.log(jsonstr);
                if(jsonstr.success) {
                    reload();
                    $.messager.alert("消息提示", "通知删除成功！", "info");
                } else {
                    $.messager.alert("消息提示", "通知删除失败！", "warning");
                }
            }
        }});
}

// 查询时重新加载分页数据
var reload = function () {
    var url = "/notice/selectPage/mobile?pageNo=1";
    if(null != noticeNo && "" != noticeNo) {
        url = url + "&noticeNo=" + noticeNo;
    }
    if(null != noticeTitle && "" != noticeTitle) {
        url = url + "&noticeTitle=" + noticeTitle;
    }
    if(null != remark && "" != remark) {
        url = url + "&remark=" + remark;
    }
    if(null != startTime && "" != startTime) {
        url = url + "&startTime=" + startTime;
    }
    if(null != endTime && "" != endTime) {
        url = url + "&endTime=" + endTime;
    }
    $('#content').panel('refresh', url);
    $.mobile.go("#p2");
    var parameter = new Object();
    if($("#noticeNo").val()!="") parameter.noticeNo = noticeNo;
    if($("#noticeTitle").val()!="") parameter.noticeTitle = noticeTitle;
    if($("#remark").val()!="") parameter.remark = remark;
    if($("#startTime").val()!="") parameter.startTime = startTime;
    if($("#endTime").val()!="") parameter.endTime = endTime;
    var jsonstr = postSyncAjax("/notice/selectPage/mobile", parameter);
    total = jsonstr.data.total;
    $("#pagination").pagination({
        total:total,
        pageSize:1,
        /*layout:['list','sep','first','prev','next','last','sep',
         'manual','sep','links'],*/
        layout:['links'],
        beforePageText:'第',
        afterPageText:'页 共{pages}页',
        loading:false,
        dispalyMsg:'当前显示{from}-{to}条记录，共{total}条记录',
        onSelectPage: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $('#content').panel("refresh", "/notice/selectPage/mobile?pageNo="+pageNumber+
                "&noticeNo="+noticeNo+"&noticeTitle="+noticeTitle+"&remark="+remark+
                "&startTime="+startTime+"&endTime="+endTime);
            $(this).pagination('loaded');
        }
    });
}